package com.sprouting.ops.service;


import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sprouting.ops.dao.OpsThirdPartySiteUserDao;
import com.sprouting.ops.dao.OpsThirdPartyUserDao;
import com.sprouting.ops.model.OpsThirdPartySiteUser;
import com.sprouting.ops.model.OpsThirdPartyUser;
import com.sprouting.ops.model.qo.OpsThirdPartySiteUserQo;
import com.sprouting.ops.model.vo.OpsThirdPartySiteUserVo;
import com.sprouting.ops.model.vo.OpsThirdPartyUserVo;
import com.sprouting.ops.utils.EntityTool;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 厂商现场人员表(OpsThirdPartySiteUser)(OPS_THIRD_PARTY_SITE_USER) 表服务实现类
 *
 * @author ：lx（长沙麓谷）
 * @date 创建时间：2025-08-03 10:28:51
 */
@Service
@Slf4j
public class OpsThirdPartySiteUserService extends ServiceImpl<OpsThirdPartySiteUserDao, OpsThirdPartySiteUser> {

    @Resource
    private OpsThirdPartySiteUserDao opsThirdPartySiteUserDao;
    @Resource
    private OpsThirdPartyUserDao opsThirdPartyUserDao;

    /**
     * 分页查询 厂商现场人员表
     * @param page 分页参数
     * @param query 查询参数
     * @return
     */
    public Page<OpsThirdPartySiteUserVo> getListPage(Page page, OpsThirdPartySiteUserQo query){
        Page<OpsThirdPartySiteUserVo> listPage = opsThirdPartySiteUserDao.getListPage(page, query);
        List<OpsThirdPartySiteUserVo> list = listPage.getRecords();
        if (CollUtil.isEmpty(list)){
            return listPage;
        }

        List<String> thirdPartyUserIdBatch = list.stream().map(OpsThirdPartySiteUserVo::getThirdPartyUserId).collect(Collectors.toList());
        List<OpsThirdPartyUserVo> thirdPartyUserList = opsThirdPartyUserDao.getByIdList(thirdPartyUserIdBatch);
        Map<String, OpsThirdPartyUserVo> map = EntityTool.toMap(thirdPartyUserList, OpsThirdPartyUserVo::getId);

        for (OpsThirdPartySiteUserVo opsThirdPartySiteUserVo : list) {
            opsThirdPartySiteUserVo.setThirdPartyUserVo(map.get(opsThirdPartySiteUserVo.getThirdPartyUserId()));
        }

        return listPage;
    }

}
